layui.use(['table', 'admin', 'ax', 'func','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var form = layui.form;

    /**
     * 管理
     */
    var BtyZxMdgz = {
        tableId: "btyZxMdgzTable"
    };

    //年月选择器
    laydate.render({
        elem: '#timeLimit',
        type: 'month',
        done: function(value, date, endDate) {
            var val = value;
            $('#timeLimit1').val(val);
            BtyZxMdgz.search();

        }
    });


    /**
     * 初始门店
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyZxMdgz/dmList");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    var kh = '';
    var kyrq = '';
    $.each(languageResult.data, function (index, val) {
        //查询需要选择的门店
        $("#name").append("<option value='" + val.yaoma + "'>" + val.dm + "</option>");
    });

    form.render('select');

    /**
     * 初始化表格的列
     */
    BtyZxMdgz.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers',align:'center', title: '序号'},
            {field: 'id', hide: true, title: ''},
            {field: 'flag', title: '月份'},
            {field: 'yaoma', title: '妖码'},
            {field: 'name', title: '门店名称'},
            {field: 'cost', title: '工资'},
            // {field: 'kemuId', title: '科目id'},
            // {field: 'keywordId', title: '关键字id'},
            {field: 'kh', title: '卡号'},
            {field: 'status', title: '状态', templet: function (d) {
                    if (d.status == '1') {
                        return "已生成";
                    } else if (d.status == '2') {
                        return "未生成";
                    }
                }},
            // {field: 'createTime', sort: true, title: ''},
            // {field: 'createUser', sort: true, title: ''},
            // {field: 'updateTime', sort: true, title: ''},
            // {field: 'updateUser', sort: true, title: ''},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyZxMdgz.search = function () {
        var queryData = {};

        queryData['flag'] = $('#timeLimit1').val();
        queryData['yaoma'] = $('#name').val();

        table.reload(BtyZxMdgz.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    BtyZxMdgz.openAddDlg = function () {
        // func.open({
        //     title: '添加',
        //     content: Feng.ctxPath + '/btyZxMdgz/add',
        //     tableId: BtyZxMdgz.tableId
        // });
        if ($('#timeLimit1').val() == ''){
            layer.msg('请选择月份！',{icon: 5,time:1000})
        } else {
            var ajax = new $ax(Feng.ctxPath + "/btyZxMdgz/getGz", function (data) {
                if (data.code == 200) {
                    Feng.success("保存成功");

                    //刷新列表
                    var queryData = {};
                    table.reload(BtyZxMdgz.tableId, {where: queryData});
                    table.reload(BtyZxMdgz.tableId);
                }else {
                    Feng.error(data.message);
                }
            }, function (data) {
                Feng.error("获取失败！" + data.responseJSON.message)
            });

            ajax.set("timeLimit", $('#timeLimit1').val());
            // ajax.set(data);
            ajax.start();

            return false;
        }


    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyZxMdgz.openEditDlg = function (data) {

          var ajax = new $ax(Feng.ctxPath + "/btyZxMdgz/addBxd", function (data) {
              if (data.code == 200) {
                  Feng.success("保存成功");

                  //刷新列表
                  var queryData = {};
                  table.reload(BtyZxMdgz.tableId, {where: queryData});
                  table.reload(BtyZxMdgz.tableId);
              }else {
                  Feng.error(data.message);
              }
          }, function (data) {
              Feng.error("添加失败！" + data.responseJSON.message)
          });

          ajax.set(data);
          ajax.start();

          return false;
      };


    /**
     * 导出excel按钮
     */
    BtyZxMdgz.exportExcel = function () {
        var checkRows = table.checkStatus(BtyZxMdgz.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyZxMdgz.onDeleteItem = function (data) {

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyZxMdgz/rebutBxd", function (data) {
                Feng.success("撤回成功!");
                table.reload(BtyZxMdgz.tableId);
            }, function (data) {
                Feng.error("撤回失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否撤回报销单?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyZxMdgz.tableId,
        url: Feng.ctxPath + '/btyZxMdgz/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        limit:20,
        cols: BtyZxMdgz.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyZxMdgz.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyZxMdgz.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyZxMdgz.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyZxMdgz.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'pass') {
            BtyZxMdgz.openEditDlg(data);
        } else if (layEvent === 'rebut') {
            BtyZxMdgz.onDeleteItem(data);
        }
    });
});
